package handler

import (
	"github.com/zeromicro/go-zero/rest/httpx"
	"month/client/common"
	"month/client/internal/logic"
	"month/client/internal/svc"
	"month/client/internal/types"
	"net/http"
)

func UploadVideoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		file, video, err := r.FormFile("video")
		if err != nil {
			return
		}
		f, img, err := r.FormFile("img")
		if err != nil {
			return
		}
		defer file.Close()
		f.Close()
		//todo 图片保存本地
		imgName, err := common.UploadImg(img, f)
		if err != nil {

		}
		//todo 视频保存本地
		videoSrc, err := common.UploadVideo(video, file)
		if err != nil {

		}
		//todo 上传阿里云
		imgUrl := common.AliFile(img, imgName, "上海八维")
		common.AliUploadVideo(videoSrc)
		r.Form.Set("img", imgUrl)
		r.Form.Set("video", " http://www.videolan.org/x264.mp4")
		var req types.UploadVideoReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := logic.NewUploadVideoLogic(r.Context(), svcCtx)
		resp, err := l.UploadVideo(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}
